//
//  RadioViewController.m
//  A_program
//
//  Created by BayMax on 15/5/22.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "RadioViewController.h"
#import "MyCollectionViewCell.h"
#import <AFNetworking/AFNetworking.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "RadioScrollView.h"
#import "RadioModel.h"
#import "RadioSecondViewController.h"
#import "RadioTouchImage.h"
#import "RadioThirdViewController.h"
#import "PlayerViewController.h"
@interface RadioViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, RadioScrollViewDelegate>

@property (nonatomic, retain) UICollectionView *radioCollection;
@property (nonatomic, retain) NSMutableArray *radioSmallArray;

@end


@implementation RadioViewController

- (void)dealloc
{
    [_radioArray release];
    [_radioCollection release];
    [_radioSmallArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.radioArray = [NSMutableArray array];
        self.radioSmallArray = [NSMutableArray array];
    }
    return self;
}

#pragma mark -
#pragma mark 网络请求数据 ---- 创建collection

- (void)web
{
    
    NSString *urlPath = @"http://mobile.ximalaya.com/m/category_tag_list?category=radio&device=iPhone&type=album";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:urlPath parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [NSArray array];
        NSMutableArray *smallArray = [NSMutableArray array];
        array = [dict objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            RadioModel *model = [[RadioModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [smallArray addObject:model];
            [model release];
        }
        self.radioArray = [NSMutableArray arrayWithArray:smallArray];
        [self.radioCollection reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载错误 is %@", error);
    }];
}


#pragma mark -
#pragma mark 网络请求数据 ---- 创建ScrollView
- (void)webb
{
    NSString *urlPath = @"http://mobile.ximalaya.com/m/category_focus_image?categoryId=17&device=iPhone&version=3.1.37";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:urlPath parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dict objectForKey:@"list"];
        
        for (NSMutableDictionary *smallDic in array) {
            RadioModel *model = [[RadioModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.radioSmallArray addObject:model];
            [model release];
        }
        
        [self createScrollView];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"下载错误 is %@", error);
        
    }];
}

//创建scrollView
- (void)createScrollView
{
    RadioScrollView *radioScroll = [[RadioScrollView alloc]initWithFrame:CGRectMake(5, 5, self.view.frame.size.width - 10, self.view.frame.size.height / 3)];
    radioScroll.delegate = self;
    [radioScroll setScrollViewIamage:self.radioSmallArray];
    [self.radioCollection addSubview:radioScroll];
    [radioScroll release];
}

#pragma mark -
#pragma mark 创建collectionView
- (void)createCollection
{
    
    UICollectionViewFlowLayout *radioLayout = [[UICollectionViewFlowLayout alloc]init];
    radioLayout.sectionInset = UIEdgeInsetsMake(self.view.frame.size.height / 3, 5, 49, 5);
    
    self.radioCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 55) collectionViewLayout:radioLayout];
    self.radioCollection.delegate = self;
    self.radioCollection.dataSource = self;
    [self.radioCollection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.radioCollection.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [self.view addSubview:self.radioCollection];
    
    [radioLayout release];
    [self.radioCollection release];
}


#pragma mark -
#pragma mark collectionView的cell数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.radioArray count];
}

#pragma mark -
#pragma mark collectionView的cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor whiteColor]];
    RadioModel *model = [self.radioArray objectAtIndex:indexPath.item];
    [cell.radioImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_path] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    cell.radioLabel.text = [NSString stringWithFormat:@"%@", model.tname];
    
    return cell;
}

#pragma mark -
#pragma mark 跳转第二页面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    RadioSecondViewController *second = [[RadioSecondViewController alloc]init];
    RadioModel *model = [self.radioArray objectAtIndex:indexPath.item];
    second.model = model;
    second.secondTname = model.tname;
    [self.navigationController pushViewController:second animated:YES];
}

#pragma mark -
#pragma mark 设置cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.frame.size.width / 3 - 15, 100);
}

- (void)imageTouch:(RadioTouchImage *)image
{
    if (image.model.albumId != nil) {
        RadioThirdViewController *third = [[RadioThirdViewController alloc]init];
        third.myId = image.model.albumId;
        [self.navigationController pushViewController:third animated:YES];
        
    } if (image.model.trackId) {
        PlayerViewController *player = [[PlayerViewController alloc] init];
        player.trackId = image.model.trackId;
        [self presentViewController:player animated:YES completion:nil];
        [player release];
    }
//    NSLog(@"sadasd");
}

- (void)loadView
{
    [super loadView];
    self.navigationItem.title = @"电台";
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    [self createCollection];
    [self web];
    [self webb];
}

- (void)viewDidLoad {
    [super viewDidLoad];


}



@end
